//step01 定义JQuery的作用域
(function($) {
	castlistLength="";
	$.fn.siySlider = {

		div:{},
		defaults : {
			url : '',
			pageSize : 10,
			page : 1,
			isSort : false,
			isSessionRequired : false,
			isPagination : true,
			data:{},
			dragToLoad:true,
			loadMoreText:"查看更多",
			emptyContentText:"没有内容"
		},
		divObjs:{},
		init : function(divObj, opt) {
			var options = {
				settings : $.extend(true,  {},$.fn.siySlider.defaults, opt)
			};
			options={
					settings : $.extend(options.settings, opt.data)
			};
			var slideObj={
					options:options,
					reload:function(opt){
						var objOption = 
								 $.extend(true,{},options.settings, opt);
							
						$.fn.siySlider.reload(divObj,objOption);
					}
			};
			$.fn.siySlider.divObjs[$(divObj).attr('id')] = slideObj;
			if(!options.settings.dragToLoad)
				$(divObj).append("<div class=\"loadMore\"><a>"+options.settings.loadMoreText+"</a></div>");
			if(options.settings.dragToLoad){
				$(document).on(
						"scrollstop",
						function() {

							
							
							if ($(document).height() > $(window).height()) {
								if ($(window).scrollTop() == $(document).height()
										- $(window).height()) {
									console.log('滚动到底部了');
									if(castlistLength==""){
										castlistLength=10;
									}
									castlistLength+=10;
									$.cookie("castlistLengthN",castlistLength);
									$.fn.siySlider.loadNext(divObj,options);
										
								}
							}
							console.log('开始滚动');
							console.log(castlistLength);
							var documentScrollTop=$(document).scrollTop();
							$.cookie("scrollTopValue",documentScrollTop);
							
						});
				
			}
			else{
				$(divObj).find(".loadMore").on("click",function(){
					$.fn.siySlider.loadNext(divObj,options);      
				});
				//$(document).animate({scrollTop:$.cookie("scrollTopValue")},1000);
			}
			$.ajax({
				type : "post",
				url : options.settings.url,
				data : options.settings,
				dataType : "text",
				
				success : function(data) {
					$(divObj).prepend(data);
					if(data==""){
						if(!options.settings.dragToLoad){
							
							$(divObj).find(".loadMore").remove();
							$(divObj).append("<div class=\"empty\"><a>"+options.settings.emptyContentText+"</a></div>");
						}
							
					}
					
				}
			});
			return slideObj;
			

		},
		loadNext:function(divObj,options){
			var options=$.fn.siySlider.divObjs[$(divObj).attr('id')].options ;
			options.settings.page+=1;
			
			$.ajax({
				type : "post",
				url : options.settings.url,
				data : options.settings,
				dataType : "text",
				beforeSend:function(){
					if(!options.settings.dragToLoad){
						$(divObj).find(".loadMore").html("<a>载入中</a>");
					}
					
				},
				success : function(data) {
					if(!options.settings.dragToLoad){
						$(divObj).find(".loadMore").before(data);
						if(data!=""){
							$(divObj).find(".loadMore").html("<a>"+options.settings.loadMoreText+"</a>");
						}
						else{
							$(divObj).find(".loadMore").html("<a>"+options.settings.emptyContentText+"</a>");
				}
					}
					else{
						$(divObj).append(data);
					}
				}
			});
		},
		reload:function(divObj,opt){
			var options = {
					 settings:$.extend(opt, opt.data)
				};
			$.fn.siySlider.divObjs[$(divObj).attr('id')].options = options;
			$.ajax({
				type : "post",
				url : options.settings.url,
				data : options.settings,
				dataType : "text",
				success : function(data) {
					
					if(data==""){
						if(!options.settings.dragToLoad){
							
							$(divObj).find(".loadMore").remove();
							$(divObj).html("<div class=\"empty\"><a>"+options.settings.emptyContentText+"</a></div>");
						}
						else{
							$(divObj).html("<div class=\"empty\"><a>"+options.settings.emptyContentText+"</a></div>");
						}
					}
					else{
						$(divObj).html(data);
					}
					
				}
			});
		}
	}
})(jQuery);